﻿Imports TMySQL
Public Class frmProcPrivileges
    Private _PrivList As TMyProcPrivilegesList
    Private Sub New()
        ' この呼び出しは、Windows フォーム デザイナで必要です。
        InitializeComponent()
        ' InitializeComponent() 呼び出しの後で初期化を追加します。
    End Sub
    Public Sub New(ByVal APrivs As TMyProcPrivilegesList)
        Me.New()
        _PrivList = APrivs
        InitFrm()
    End Sub
    Private Sub InitFrm()
        lstDatabase.Items.Clear()
        For i As Integer = 0 To _PrivList.Count - 1
            If Not lstDatabase.Items.Contains(_PrivList(i).Db) Then
                lstDatabase.Items.Add(_PrivList(i).Db)
            End If
        Next
    End Sub

    Private Sub lstDatabase_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstDatabase.SelectedIndexChanged
        Cursor.Current = Cursors.WaitCursor
        lvProc.SuspendLayout()
        Try
            lvProc.Items.Clear()
            If lstDatabase.SelectedItems.Count <= 0 Then
                Return
            End If
            ShowRoutines(lstDatabase.SelectedItem)
        Finally
            lvProc.ResumeLayout()
            Cursor.Current = Cursors.Default
        End Try

    End Sub
    Private Sub ShowRoutines(ByVal ADatabaseName As String)
        lvProc.Items.Clear()
        For i As Integer = 0 To _PrivList.Count - 1
            If _PrivList(i).Db.Equals(ADatabaseName) Then
                Dim li As New ListViewItem
                li.Text = _PrivList(i).RoutineName
                li.SubItems.Add(_PrivList(i).RoutineType)
                li.SubItems.Add(Common.DateTimeToString(_PrivList(i).Created))
                li.SubItems.Add(Common.DateTimeToString(_PrivList(i).LastAltered))
                li.SubItems.Add(_PrivList(i).Grantor)
                li.SubItems.Add(_PrivList(i).RoutineComment)
                li.Tag = _PrivList(i)
                lvProc.Items.Add(li)
            End If
        Next
        For Each col As ColumnHeader In lvProc.Columns
            col.Width = -2
        Next
    End Sub

    Private Sub InitCheck()
        chkAlterRoutine.Checked = False
        chkExecute.Checked = False
        chkGrantOption.Checked = False
    End Sub

    Private Sub lvProc_ItemSelectionChanged(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ListViewItemSelectionChangedEventArgs) Handles lvProc.ItemSelectionChanged
        Cursor.Current = Cursors.WaitCursor
        Try
            InitCheck()
            If Not e.IsSelected Then
                Return
            End If
            ShowPrivs(e.Item.Tag)
        Finally
            Cursor.Current = Cursors.Default
        End Try
    End Sub

    Private Sub ShowPrivs(ByVal APriv As TMyProcPrivileges)
        chkAlterRoutine.Checked = APriv.AlterRoutinePriv
        chkExecute.Checked = APriv.ExecutePriv
        chkGrantOption.Checked = APriv.GrnatPriv
    End Sub

End Class